<template>
  <div class="navbar">
    <hamburger id="hamburger-container" :is-active="isCollapse" class="hamburger-container"
      @toggleClick="toggleSideBar" />
      <breadcrumb id="breadcrumb-container" class="breadcrumb-container"/>
    <div class="right-menu">
      <div>消息</div>
      <div>
        <screenfull id="screenfull" class="right-menu-item hover-effect" />
      </div>
      <div class="avatar-container">
        <el-dropdown @command="handleCommand" class="right-menu-item hover-effect name" trigger="hover">
          <div class="avatar-wrapper">
            <div class="userName">欢迎您：{{ name }}</div>
            <el-icon class="el-input__icon"><arrowDown /></el-icon>
          </div>
          <template #dropdown>
            <el-dropdown-menu>
              <router-link to="/user/profile">
                <el-dropdown-item>个人中心</el-dropdown-item>
              </router-link>
              <el-dropdown-item divided command="logout">
                <span>退出登录</span>
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ElMessageBox } from 'element-plus';
import { computed,ref } from "vue";
import { ArrowDown } from '@element-plus/icons-vue'
import Hamburger from "@/components/Hamburger";
import Breadcrumb from "@/components/Breadcrumb";
import Screenfull from "@/components/Screenfull";
import { AppStore } from "@/store/modules/app";
import { UserStore } from "@/store/modules/user";
const appStore = AppStore()
const userStore = UserStore()
const name = ref(userStore.name)
const isCollapse = computed(() => appStore.sidebar.opened);
function toggleSideBar() {
  // store.dispatch('app/toggleSideBar');
  appStore.toggleSidebar()
}

function handleCommand(command) {
  switch (command) {
    case 'setLayout':
      setLayout();
      break;
    case 'logout':
      logout();
      break;
    default:
      break;
  }
}

function logout() {
  ElMessageBox.confirm('确定注销并退出系统吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(() => {
      userStore.LogOut().then(()=> {
        location.href = "/index";
      })
    })
    .catch(() => {});
}

const emits = defineEmits(['setLayout']);
function setLayout() {
  emits('setLayout');
}
</script>

<style lang="scss" scoped>
@import '@/assets/styles/mixin.scss';
.navbar {
  @include groundGlass;
  height: 50px;
  overflow: hidden;
  position: relative;
  background: #fff;
  // border-bottom: 0.5px solid #e5e5e5;
  box-shadow: 0 5px 5px 0 rgb(0 0 0 / 52%), 0 5px 5px 0 rgb(0 0 0 / 54%);

  .hamburger-container {
    line-height: 46px;
    height: 100%;
    float: left;
    cursor: pointer;
    transition: background 0.3s;
    -webkit-tap-highlight-color: transparent;

    &:hover {
      background: rgba(0, 0, 0, 0.025);
    }
  }

  .breadcrumb-container {
    float: left;
  }

  .topmenu-container {
    position: absolute;
    left: 50px;
  }

  .errLog-container {
    display: inline-block;
    vertical-align: top;
  }

  .right-menu {
    float: right;
    height: 100%;
    line-height: 50px;
    display: flex;
    justify-content: center;
    align-items: center;
    &:focus {
      outline: none;
    }
    .name {
      display: flex;
      justify-content: center;
      align-items: center;
    }
    .right-menu-item {
      padding: 0 8px;
      height: 100%;
      font-size: 16px;
      color: #5a5e66;
      vertical-align: text-bottom;

      &.hover-effect {
        cursor: pointer;
        transition: background 0.3s;

        &:hover {
          background: rgba(0, 0, 0, 0.025);
        }
      }
    }

    .avatar-container {
      margin-right: 15px;
      height: 100%;
      .avatar-wrapper {
        position: relative;
        display: flex;
        .user-avatar {
          cursor: pointer;
          width: 32px;
          height: 32px;
          border-radius: 40px;
        }

        i {
          cursor: pointer;
          right: -6px;
          top: 2px;
          font-size: 12px;
        }
      }
    }
  }
}
</style>
